import csv

class Student:
    def __init__(self,no,name,age,major):
        self.no = no
        self.name = name
        self.age = age
        self.major = major

stus = [
    Student('01','a',12,'a'),
    Student('02','b',12,'b'),
    Student('03','c',12,'c')
]

with open('stu.csv','w',encoding='utf-8',newline='') as f:
    fn = ['学号','姓名','年龄','专业']
    writer = csv.DictWriter(f,fieldnames=fn)
    writer.writeheader()

    for s in stus:
        d = {'学号':s.no,'姓名':s.name,'年龄':s.age,'专业':s.major}
        writer.writerow(d)

